package com.weipai.springboot.module.controller;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Picture;
import com.weipai.springboot.model.PictureCategory;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.FileUploadService;
import com.weipai.springboot.module.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.Id;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/27 17:26
 * 
 * 图片控制类
 */
@RestController
public class PictureController {
    
    @Autowired
    PictureService pictureService;
    @Autowired
    FileUploadService fileUploadService;

    /**
     * 通过图片的分类获取图片
     * @param id
     * @return
     */
    @GetMapping("/picture/getImgByPictureCategoryId/{id}")
    public Response getImgByPictureCategoryId(@PathVariable("id") String id){
        return pictureService.getImgByPictureCategoryId(id);
    }

    /**
     * 图片的保存
     * @param picture
     * @return
     */
    @PostMapping("/picture/savePicture")
    public Response savePicture(HttpServletRequest request,Picture picture, MultipartFile img){
        Response response = new Response();
        if(img!=null){
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if(result.equals("error")){
                response.setCode(RetCode.IMG_ERROR);
            }else{
                picture.setPictureUrl(result);
                response = pictureService.savePicture(picture);
            }
        }else{
            response = pictureService.savePicture(picture);
        }
        return response;
    }

    /**
     * 通过图片id获取图片详情
     * @param id
     * @return
     */
    @GetMapping("/picture/getPictureById/{pictureId}")
    public Response getPictureById(@PathVariable("pictureId") Integer id){
        return pictureService.getPictureById(id);
    }

    /**
     * 图片的删除
     * @param id
     * @return
     */
    @GetMapping("/picture/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id") Integer[] id){
        return pictureService.batchDeleteById(id);
    }

    /**
     * 图片的修改
     * @param picture
     * @return
     */
    @PostMapping("/picture/updatePicture")
    public Response updatePicture(HttpServletRequest request,Picture picture,MultipartFile img){
        Response response = new Response();
        if(img!=null){
                String result = fileUploadService.saveImgUpload(request, img);
                if (result.equals("formatError")) {
                    response.setCode(RetCode.IMG_FORMAT);
                } else if (result.equals("sizeover")) {
                    response.setCode(RetCode.IMG_SIZE);
                } else if(result.equals("error")){
                    response.setCode(RetCode.IMG_ERROR);
                }else{
                    picture.setPictureUrl(result);
                    response = pictureService.updatePicture(picture);
                }
        }else{
            response = pictureService.updatePicture(picture);
        }
        return response;
    }

    /**
     * 图片分类的保存
     * @param pictureCategory
     * @return
     */
    @PostMapping("/picture/savePictureCategory")
    public Response savePictureCategory(PictureCategory pictureCategory){
        return pictureService.savePictureCategory(pictureCategory);
    }

    /**
     * 图片分类的修改
     * @param categoryName：分类的名称
     * @param id：分类主键
     * @return
     */
    @GetMapping("/picture/updatePictureCategory/{id}/{categoryName}")
    public Response updatePictureCategory(@PathVariable("id") Integer id, @PathVariable("categoryName") String categoryName){
        return pictureService.updatePictureCategory(id, categoryName);
    }

    /**
     * 获取图片分类的详情
     * @param id：分类主键
     * @return
     */
    @GetMapping("/picture/getPictureCategoryInfo/{id}")
    public Response getPictureCategoryInfo(@PathVariable("id") Integer id){
        return pictureService.getPictureCategoryInfo(id);
    }

    /**
     * 图片分类的删除
     * @param id
     * @return
     */
    @PostMapping("/picture/batchDeleteByCategoryId")
    public Response batchDeleteByCategoryId(Integer[] id){
        return pictureService.batchDeleteByCategoryId(id);
    }

    /**
     * 图片分类列表
     * @return
     */
    @GetMapping("/picture/pictureCategoryList")
    public Response pictureCategoryList(){
        return pictureService.pictureCategoryList();
    }
    
    
    
    
    
    
}
